#include "string.h"
#include "stdint.h"
#include "LsdLinkList.h"
#include "LsdMem.h"

TypeLinkedInfo *OnAddLinkedInfoValue(uint16_t type, int32_t value)
{
    TypeLinkedInfo *linkedInfo = (TypeLinkedInfo *)pvPortMalloc(sizeof(TypeLinkedInfo));
    linkedInfo->type = type;
	  linkedInfo->value = value;
    linkedInfo->dataLen = 0;
    linkedInfo->data = NULL;
	  linkedInfo->next = NULL;
    return linkedInfo;
}

TypeLinkedInfo *OnAddLinkedInfo(uint16_t type, uint8_t *data, uint16_t datalen)
{
    TypeLinkedInfo *linkedInfo = 0;
    if(data && datalen)
    {
        linkedInfo = (TypeLinkedInfo *)pvPortMalloc(sizeof(TypeLinkedInfo));
        linkedInfo->type = type;
				linkedInfo->value = 0;
			  linkedInfo->next = NULL;
        linkedInfo->dataLen = datalen;
        linkedInfo->data = (uint8_t *)pvPortMalloc(datalen + 1);
        memcpy(linkedInfo->data, data, linkedInfo->dataLen);
    }
    return linkedInfo;
}

uint8_t OnAddBuffsToList(TypeLinkedList *list, uint8_t *tbuff, uint16_t tlen)
{
	return OnAddLinkedToList(list, OnAddLinkedInfo(0, tbuff, tlen));
}

uint8_t OnAddBuffsToListDeleteSame(TypeLinkedList *list, uint8_t type, uint8_t *tbuff, uint16_t tlen)
{
		//这里要查找一下,如果里面有一样的就删除
	TypeLinkedInfo *pLast = list->pHead;
	TypeLinkedInfo *pNext = pLast;
	TypeLinkedInfo *pInfo = NULL;
	while(pNext && list->count)
	{
		if(pNext->type == type)
		{
			pInfo = pNext;
			pNext = pNext->next;
			if(list->pHead == pInfo)
			{
				pLast = list->pHead = pNext;
			}
			else if(list->pTail == pInfo)
			{
				list->pTail = pLast;
				pLast->next = NULL;
			}
			else
			{
				pLast->next = pNext;
			}
			//释放pInfo
			if(pInfo->data) vPortFree(pInfo->data);
			vPortFree(pInfo);
			if(list->count) list->count--;
			if(list->count == 0)
			{
				list->pTail = NULL;
			}
		}
		else
		{
			if(pNext != pLast)
			{
				pLast = pLast->next;
			}
			pNext = pNext->next;
		}
	}
	//添加
	return OnAddLinkedToList(list, OnAddLinkedInfo(type, tbuff, tlen));
}

uint8_t OnAddLinkedToList(TypeLinkedList *list, TypeLinkedInfo *linkedinfo)
{
    if(list && linkedinfo)
    {
        if(list->pHead == 0)//添加到头部
        {
            //这里是第一次添加
            list->pHead = linkedinfo;
        }
        else if(list->pHead->next == 0)
        {
            //这里应该是第二次添加
            list->pTail = linkedinfo;
            list->pHead->next = list->pTail;
        }
        else
        {
            //第三次添加
            list->pTail->next = linkedinfo;
            list->pTail = list->pTail->next;
        }
        return list->count++;
    }
		else
		{
			if(linkedinfo)
			{
				//释放内存
				if(linkedinfo->data) vPortFree(linkedinfo->data);
				vPortFree(linkedinfo);
			}
		}
    return 0;
}

void OnClearLinkList(TypeLinkedList *list)
{
    if(list)
    {
        TypeLinkedInfo *listInfo = 0;
        while (list->pHead)
        {
            listInfo = list->pHead;
            list->pHead = list->pHead->next;
            if(listInfo->data)
            {
                vPortFree(listInfo->data);
            }
            vPortFree(listInfo);
            if(list->count) list->count--;
        }
    }
}
